//
//  BooKViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 12-12-12.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "BooKViewController.h"
#import "DoctorsSeriesViewController.h"


static ASIHTTPConnection *forumDepartment_connect;


@interface BooKViewController ()

@end

@implementation BooKViewController

- (void)dealloc{
    
    if (forumDepartment_connect != nil) {
        forumDepartment_connect.connectionDelegate = nil;
        [forumDepartment_connect release];
        forumDepartment_connect = nil;
    }
    
    [myArray release];
    [myTableView release];
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewWillAppear:(BOOL)animated{
    
     [self loadDepartmentData];
    
    [super viewWillAppear:NO];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    [self loadDepartmentData];
    
    if (myArray == nil) {
        myArray = [[NSMutableArray alloc]init];
    }
    
    // Do any additional setup after loading the view from its nib.
}


- (void)loadDepartmentData{
    
    NSString *urlString = [NSString stringWithFormat:@"%@/selectAppointmentDepartment",UCENTERBASEURL];
	NSURL *url = [NSURL URLWithString:urlString];
    
    if (forumDepartment_connect != nil) {
        forumDepartment_connect.connectionDelegate = nil;
        [forumDepartment_connect release];
        forumDepartment_connect = nil;
    }
    
    forumDepartment_connect = [[ASIHTTPConnection alloc] initGETWithURL:url resultDict:YES isCache:YES];
    forumDepartment_connect.connectionDelegate = self;
    
    [forumDepartment_connect startAsynchronous];
    
    
}



#pragma mark connectino delegate
- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    NSArray *array = [dict objectForKey:@"departmentlist"];
    
    if (myArray != nil) {
        [myArray removeAllObjects];
    }
    
    [myArray addObjectsFromArray:array];
    [myTableView reloadData];
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return myArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MyIdentifier = @"forumCell";
    
    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] init] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
	    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)];
        [titleLable setTextAlignment:NSTextAlignmentLeft];
        [titleLable setTextColor:[UIColor blackColor]];
        titleLable.backgroundColor = [UIColor clearColor];
        titleLable.tag = 100;
        [cell addSubview:titleLable];
        
        [titleLable release];
        
        
    }
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
    
    NSString *string = [dict objectForKey:@"lab_Name"];
    UILabel *lable = (UILabel *)[cell viewWithTag:100];
    lable.text = string;
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
      
    
}


@end
